﻿using FlowerModel;
using Interface;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FlowerDAL
{
    public class FlowersDAL : IFlowers
    {

        #region 显示方法
        /// <summary>
        /// 显示方法
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public List<T> GetFlowers<T>()
        {
            return DBHelper.GetList<T>();
        }
        #endregion

        #region 添加花卉

        /// <summary>
        /// 添加花卉
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="flo"></param>
        /// <returns></returns>
        public bool AddStock1<T>(T flo)
        {
            return DBHelper.InsertEntity<T>(flo);
        }
        #endregion

        #region 花卉修改状态

        /// <summary>
        /// 花卉修改状态
        /// </summary>
        /// <param name="state"></param>
        /// <param name="id"></param>
        /// <returns></returns>
        public int UpdateStateFlowers(int state, int id)
        {
            if (state == 1)
            {
                state = 0;
            }
            else
                state = 1;

            string sql = "update FlowersMasters set State=" + state + " where Id=" + id;
            return DBHelper.ExecuteNonQuery(sql);
        }

        #endregion

        #region  采购商品

        /// <summary>
        /// 采购商品
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="flo"></param>
        /// <returns></returns>
        public bool PurchaseStock<T>(T flo)
        {
            return DBHelper.UpdateEnity<T>(flo);
        }

        #endregion

        #region 反填数据

        /// <summary>
        /// 反填数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public T GetStock<T>(int id)
        {
            return DBHelper.GetEntity<T>(id);
        }

        #endregion

        #region 价钱合计
        //进货价钱合计

        public int GetMoney()
        {
            string sql = "select sum(StockPrice*Num) heji  from dbo.FlowersMasters where StockId=0";

            if (DBHelper.ExecuteScalar(sql) is DBNull)
            {
                return 0;
            }
            else
                return Convert.ToInt32(DBHelper.ExecuteScalar(sql));
        }

        //进货详情价钱合计
        public int GetXqMoney(int? StockId)
        {
            string sql = "select sum(StockPrice*Num) heji  from FlowersMasters where 1=1  and StockId=" + StockId;
            return Convert.ToInt32(DBHelper.ExecuteScalar(sql));
        }
        #endregion

        #region 查询
        /// <summary>
        /// 按花的种类条件查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="Parameter"></param>
        /// <returns></returns>

        public List<T> GetStockList<T>(params System.Data.SqlClient.SqlParameter[] Parameter)
        {
            string sql = DBHelper.SelGetlist<T>(Parameter);
            DataTable dt = DBHelper.ExecuteDataTable(sql);
            List<T> t = new List<T>();
            foreach (DataRow item in dt.Rows)
            {
                Type t1 = typeof(T);

                T t2 = (T)t1.Assembly.CreateInstance(t1.FullName);
                foreach (var i in t1.GetProperties())
                {

                    i.SetValue(t2, item[i.Name]);

                }
                t.Add(t2);

            }
            return t;

        }
        #endregion

        #region 采购订单显示
        //采购订单显示
        public List<FlowersMasters> Getlist()
        {
            string sql = "select * from dbo.FlowersMasters  where StockId=0";
            return DBHelper.ExecuteObjects<FlowersMasters>(sql);
        }
        #endregion

        #region 实现批量修改
        //批量修改FlowersMasters表的StockId字段
        public int UpdateFlowers(int stockid, string str)
        {

            string sql = "update FlowersMasters set StockId=" + stockid + " where id in (" + str + ")";
            return DBHelper.ExecuteNonQuery(sql);
        }

        //批量修改库存数量
        public int UpdateNum(List<Flowers> list, string flowername, string num)
        {
        
            string sql = "";
            string[] flower = flowername.Split(',');
            string[] nums = num.Split(',');
            for (int i = 0; i < flower.Length; i++)
            {
                for (int j = 0; j < list.Count; j++)
                {
                    if (flower[i] == list[j].FlowersName)
                    {
                        sql = "update dbo.Flowers set KNum +=" + nums[i] + " where FlowersName ='" + flower[i] + "'";
                        DBHelper.ExecuteNonQuery(sql);
                        
                    }
                    else
                    {
                        continue;
                    }


                }
            }
            return 1;

        }
        #endregion

        #region 获取进货订单的订单号

        //获取进货订单的订单号
        public string GetStockId()
        {
            string sql = "select max(StockId) from Stock";
            return DBHelper.ExecuteScalar(sql).ToString();
        }
        #endregion

        /// <summary>
        /// 添加购物车
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int AddMyCar(int id)
        {
            string sql = "insert into MyCar values('" + id + "',default)";
            return DBHelper.ExecuteNonQuery(sql);
        }
        /// <summary>
        /// 购物车显示
        /// </summary>
        /// <returns></returns>
        public List<MyCar> MyCarList()
        {
            string sql = "select * from Flowers a join MyCar b on a.Id=b.FlowersId";
            return DBHelper.ExecuteObjects<MyCar>(sql);
        }
        /// <summary>
        /// 通过id查询出购买数量
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int UpdateNum(int id)
        {
            string sql = "select ByNum from MyCar where Id=" + id;
            return Convert.ToInt32(DBHelper.ExecuteScalar(sql));
        }
        /// <summary>
        /// 修改库存
        /// </summary>
        /// <param name="buy"></param>
        /// <returns></returns>
        public int UpdateKNum(MyCar my)
        {
            string sql = "update MyCar set ByNum=" + my.ByNum + " where Id=" + my.Id + "";
            return DBHelper.ExecuteNonQuery(sql);
        }
        /// <summary>
        /// 添加订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int AddOrder(int?id)
        {
            string sql = "insert into SellIdentMaster values('" + id + "')";
            return DBHelper.ExecuteNonQuery(sql);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete<T>(int id)
        {
            return DBHelper.DeleteEntity<T>(id);
        }
        int IFlowers.Delete<T>(int id)
        {
            throw new NotImplementedException();
        }
    }
}
